<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :title="`${type === 'Add' ? '新增' : '编辑'}角色`"
    width="600px"
    @close="handleClose"
    custom-class="dialog"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" >
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="form.name"/>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.remark" type="textarea" :rows="6" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false" size="small">取 消</el-button>
      <el-button type="primary" @click="submitForm" size="small"
        >确 定
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
import {addRole,editRole} from '@/api/role'
export default {
  
  data() {
    return {
      type: "Add",
      currentRow: null,
      dialogVisible: false,
      form: {
        name: "",
        remark: "",
      },
      rules: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" }
        ],
      },
    };
  },
  methods: {
    openHandle(type, row) {
      this.type = type;
      this.currentRow = { ...row };
      if(type=='Edit'){
        this.form=row
      }
      this.dialogVisible = true;
    },
    handleClose() {
      this.$refs["form"].resetFields();
      this.form = {
        name: "",
        remark: "",
      };
      this.dialogVisible = false;
    },
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          const {projectId}=this.$store.getters;
          this.form.projectId=projectId
          if(this.form.id){
            editRole(this.form).then((response) => {
              this.$message.success("修改成功");
              this.dialogVisible = false;
              this.$emit('getList')
            });
          }else{
            addRole(this.form).then((response) => {
              this.$message.success("添加成功");
              this.dialogVisible = false;
              this.$emit('getList')
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
